package com.web.cms.ad.provider;

import com.blue.core.dict.State;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.cms.ad.exception.AdvPosException;
import com.web.cms.ad.facade.AdvPosFacade;
import com.web.cms.ad.message.AdvPosRequest;
import com.web.cms.ad.message.AdvPosResponse;
import com.web.cms.ad.model.AdvPos;
import com.web.cms.ad.service.AdvPosService;
import com.web.cms.ad.util.AdvPosConvertor;
import com.web.cms.doc.cache.WebCache;
import com.web.cms.doc.model.Web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告位置接口实现类
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年11月13日
 */
@Component("adAdvPosProvider")
public class AdvPosProvider implements AdvPosFacade
{
	@Autowired
	private AdvPosService advPosService;
	@Autowired
	private WebCache webCache;
	
	public AdvPosProvider()
	{
	}

	@Override
	public AdvPosResponse add(AdvPosRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		AdvPos advPos = AdvPosConvertor.toAdvPos(request);
		advPos = advPosService.save(advPos);
		AdvPosResponse response = AdvPosConvertor.toResponse(advPos);
		return response;
	}

	@Override
	public AdvPosResponse update(AdvPosRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		AdvPos advPos = AdvPosConvertor.toAdvPos(request);
		advPos = advPosService.save(advPos);
		AdvPosResponse response = AdvPosConvertor.toResponse(advPos);
		return response;
	}

	@Override
	public AdvPosResponse get(Integer id)
	{
		if (id == null)
			throw new AdvPosException("Id不能为空");
			
		AdvPos advPos = advPosService.get(id);
		AdvPosResponse response = AdvPosConvertor.toResponse(advPos);
		return response;
	}

	@Override
	public Page<AdvPosResponse> listPage(AdvPosRequest request, int start, int size)
	{
		AdvPos advPos = AdvPosConvertor.toAdvPos(request);
		Page<AdvPos> page = new Page<>(size);
		page.setCurPage(start);
		page = advPosService.list(advPos, page);
		Page<AdvPosResponse> pageResponse = AdvPosConvertor.toResponse(page);
		return pageResponse;
	}

	@Override
	public List<AdvPosResponse> listAll(Integer webId)
	{
		if (webId == null)
			throw new AdvPosException("网站ID不能为空");

		AdvPos pos = new AdvPos();
		pos.setWebId(webId);
		pos.setState(State.NORMAL);
		List<AdvPos> list = advPosService.list(pos);
		List<AdvPosResponse> responseList = AdvPosConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public List<AdvPosResponse> listByWebCode(String webCode)
	{
		if (webCode == null || webCode.isEmpty())
			throw new AdvPosException("网站代码不能为空");

		Web web = webCache.getByCode(webCode);
		if (web == null)
			return new ArrayList<>();

		return this.listAll(web.getId());
	}

}
